;layui.define(["dropdown","table", "form"], function(exports) {
    var $ = layui.$
        ,table = layui.table
        ,form = layui.form
        ,dropdown = layui.dropdown
        ,tableId = "user_list_table"
        ,searchFormId = "#user-list-search-form";
    var tableIns = table.render({
        id:tableId,
        elem: "#"+tableId,
        url: '/sso/admin/user/userList',
        method:'post',
        parseData:function (res) {
            return{
                "code": res.status?res.status:0, //解析接口状态
                "msg": "", //解析提示文本
                "count": res.total, //解析数据长度
                "data": res.records //解析数据列表
            }
        },
        cols: [[{
            type: "checkbox",
            fixed: "left",
            width: "5%",
        }, {
            field: "id",
            width: "5%",
            title: "编号",
            sort: true
        }, {
            field: "userAccount",
            title: "账户",
            width: "15%"
        }, {
            field: "phone",
            title: "手机",
            width: "10%"
        }, {
            field: "email",
            title: "邮箱",
            width: "15%"
        }, {
            field: "realName",
            title: "真实姓名",
            width: "10%"
        },{
            field: "roleList",
            title: "角色",
            width: "20%",
            templet: function (d) {
                var roleNames = "";
                var list = d.roleList;
                for (var role in list){
                    roleNames += '<a class="layui-btn layui-btn-primary layui-btn-xs">'+list[role].roleTips+'</a>';
                }
                return roleNames;
            }
        },{
            field:'enable',
            title:'是否锁定',
            width: '10%',
            templet: '#enableTpl'
        }, {
            title: "操作",
            width: "15%",
            align: "center",
            fixed: "right",
            toolbar: "#table-useradmin-webuser"
        }]],
        page: true,
        limit: 30,
        text: {
            none: '暂无相关数据' //默认：无数据
        },
        done:function (res, curr, count) {
            this.where ={};
            if(res.code == 401){
                layer.msg("没有操作权限,请联系管理员");
            }
            if(res.code == 1001){
                layer.open({
                    title: '提示信息'
                    ,content: '登录失效,请重新登录！',
                    yes:function(index,layero){
                        layer.close(index);
                    },
                    end: function(index, layero){
                        window.location.href="/sso/logout";
                    },
                    time:5000
                });
            }
        }
    });
    table.on("tool("+tableId+")", function (e) {
        var row = e.data;
        if ("del" === e.event){
            dropdown.hideFixed();
            layer.prompt({
                formType: 0,
                title: "敏感操作，输入口令"
            }, function (value, index) {
                if(value == 'ok'){
                    layer.close(index);
                    layer.confirm('确定删除行吗？', function (t) {
                        $.get("/sso/admin/user/remove",{ids:row.id},function(data){
                            if(data.status == 200){
                                e.del(), layer.close(t),layer.msg("删除成功"),tableIns.reload();
                            }else{
                                layer.close(t),layer.msg("删除失败，请重试");
                            }
                        });
                    });
                }else{
                    layer.msg('口令错误');
                }
            });
        }
        if ("edit" === e.event) {
            $(e.tr);
            layer.open({
                type: 2,
                title: "编辑用户 EDIT",
                content: '/sso/admin/user/toEditPage?id='+row.id,
                maxmin: !0,
                area: ["500px", "600px"]
            })
        }
        if("detail" === e.event){
            dropdown.hideFixed();
            var url = "user/edit";
            layer.open({
                type: 2,
                title: "用户详情 DETAIL",
                content: '/sso/admin/user/toEditPage?id='+row.id+"&type=detail",
                maxmin: !0,
                area: ["500px", "450px"]
            })
        }
        if("allot" === e.event){
            layer.open({
                type: 2,
                title: "角色分配 AllOT",
                content: '/sso/admin/user/toGrantRolesPage?userId='+row.id,
                maxmin: !0,
                area: ["500px", "450px"],
                end:function(index, layero){
                    var field = $(searchFormId).serializeObject();
                    tableIns.reload({
                        where: field
                    });
                }
            });
        }
    });

    //监听锁定操作
    form.on('switch(userEnableSwitch)', function(obj){
        var value  = 2;
        var returnText = "用户禁用成功！";
        var tipText = '确定禁用当前用户？';
        if(obj.elem.checked){
            tipText = '确定启用当前用户？'
            returnText = "用户启用成功！";
            value = 1;
        }
        var json = JSON.parse(decodeURIComponent($(this).data('json')));
        json = table.clearCacheKey(json);
        layer.confirm(tipText,{
            icon: 3,
            title:'系统提示',
            yes: function (index) {
                $.get("/sso/admin/user/userSwitch",{enable:value,userId:json.id},function(res){
                    if(res.code == '00000'){
                        layer.close(index),layer.msg(returnText);
                    }else{
                        obj.elem.checked = !obj.elem.checked;
                        form.render();
                        layer.close(index),layer.msg(res.msg);
                    }
                });
            },
            cancel : function(index){
                var x = obj.elem.checked;
                obj.elem.checked = !x;
                form.render();
                layer.close(index);
            }
        });
    });

    //事件
    var active = {
        batchDel: function () {
            var checkStatus = table.checkStatus(tableId)
                , checkData = checkStatus.data
            var deList = []; //得到选中的数据
            if (checkData.length === 0) {
                return layer.msg('请选择数据');
            }
            checkData.forEach(function (row,index) {
                deList.push(row.id);
            });

            layer.prompt({
                formType: 0
                , title: '敏感操作，请验证口令'
            }, function (value, index) {
                if(value == 'ok'){
                    layer.close(index);
                    layer.confirm('确定删除吗？', function (index) {
                        $.get("/sso/admin/user/remove",{ids:deList.join()},function(data,status){
                            if(data.status == 200){
                                tableIns.reload(tableId);
                                layer.msg("删除成功");
                            }else{
                                layer.msg("删除失败，请重试");
                            }
                        });

                    });
                }else{
                    layer.msg('口令错误');
                }

            });
        }
        , add: function () {
            var url = "user/add";
            layer.open({
                type: 2,
                title: '添加用户 ADD',
                content: '/sso/admin/forward/systemManage?url='+url,
                maxmin: true,
                area: ['500px', '600px']
            });
        },
        reload: function () {
            var field = $(searchFormId).serializeObject();
            tableIns.reload({
                where: field
            });
        },
        reset: function () {
            tableIns.reload();
        }
    };
    $('.toolbar-button').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    exports("useradmin", {});
});
